package main

import (
	"context"
	"database/sql"
	"errors"
	"fmt"

	"gitee.com/zhangxinzx/wrap-error/pkg/dal"
	"gitee.com/zhangxinzx/wrap-error/pkg/database"
	"gitee.com/zhangxinzx/wrap-error/pkg/model"
	"github.com/google/uuid"
	xerrors "github.com/pkg/errors"
)

func main() {
	db, err := database.NewSqlite3DB("file:testdata/userdao/test.sqlite")
	if err != nil {
		fmt.Println("main: create sqlite3 db failed with ", err)
		return
	}
	defer db.Close()

	_, err = db.ExecContext(context.Background(), dal.UserTable)
	if err != nil {
		fmt.Println("main: create sqlite3 table failed with ", err)
		return
	}

	udao := dal.NewUserDAO(db, func() string {
		return uuid.New().String()
	})
	u := &model.User{FirstName: "xin", LastName: "zhang", Age: 18, Gender: "male"}
	entity, err := udao.Create(context.Background(), u)
	if err != nil {
		fmt.Println("main: create user failed with ", err)
		return
	}
	fmt.Println("entity uuid: ", entity.Uuid(), " entity value: ", entity.Value().(*model.User))

	_, err = udao.GetByID(context.Background(), uuid.New().String())
	if err != nil {
		fmt.Println("\nIs sql.ErrNoRows: ", errors.Is(err, sql.ErrNoRows), "\nCause equal: ", xerrors.Cause(err) == sql.ErrNoRows, "\nError msg: ", err)
		return
	}
}
